package com.otp.controller;

import com.otp.pojo.dto.ResourceDto;
import com.otp.pojo.entity.Resource;
import com.otp.pojo.request.resource.*;
import com.otp.service.ResourceService;
import com.otp.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping
    public ResponseMessage<List<ResourceDto>> list() {
        return ResponseMessage.success(resourceService.list()
                .stream()
                .map(Resource::convertToDto)
                .toList());
    }

    @GetMapping("/category/{category}")
    public ResponseMessage<List<ResourceDto>> listByCategory(@PathVariable String category) {
        return ResponseMessage.success(resourceService.listByCategory(category)
                .stream()
                .map(Resource::convertToDto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<ResourceDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(resourceService.getById(id)
                .convertToDto());
    }

    @PostMapping
    public ResponseMessage<ResourceDto> create(@ModelAttribute ResourceCreateRequest request) {
        return ResponseMessage.success(resourceService.create(request)
                .convertToDto());
    }

    @PutMapping
    public ResponseMessage<ResourceDto> update(@ModelAttribute ResourceUpdateRequest request) {
        return ResponseMessage.success(resourceService.update(request)
                .convertToDto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        resourceService.delete(id);
        return null;
    }
}